﻿using System;
using System.Web.Mvc;

namespace Entropy.Ext
{
	public static class HtmlHelperExtensions
	{
		public static IExtFactory Ext(this WebViewPage webViewPage, FormatterKind formatter = FormatterKind.Debug)
		{

			var result = webViewPage.Context.Items["EntropyHolder"] as IExtFactory;
			if (result == null)
			{
				var fmt = FormatterFactory(formatter);
				ISyntaxTree syntaxTree = new SyntaxTree();
				IAsgBuilder asg = new AsgBuilder(new ExtAccent(), new SequentialTypeNaming());
				result = ExtFactoryProxy.Create(webViewPage, syntaxTree, () => fmt.Format(asg.Compile(syntaxTree.Result())));


				webViewPage.Context.Items["EntropyHolder"] = result;
			}
			return result;
		}

		private static IJsFormatter FormatterFactory(FormatterKind formatter)
	    {
	        DebugFormatter debugFormatter;
	        switch (formatter)
	        {
	            case FormatterKind.Debug:
	                debugFormatter = new DebugFormatter();
	                break;
	            default:
	                throw new Exception("unimlemented formatter");
	        }
	        return debugFormatter;
	    }
	}

    public enum FormatterKind
    {
        Debug,
        Minified,
        Obfuscate
    }
}